﻿
using Newtonsoft.Json;
using RTW.OA.Areas.System.Servers;
using RTW.OA.Controllers;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace RTW.OA.Areas.System.Controllers
{
    public class UserController : BaseController
    {
        private UserServers userServers;

        public UserController()
        {
            userServers = new UserServers();
        }


        // GET: System/User
        public ActionResult Index()
        {
            ViewBag.DEPT_IDData = JsonConvert.SerializeObject(userServers.GetDEPT_ID());
            return View();
        }

        // GET: System/User/Edit/5
        public ActionResult Edit(int? id)
        {
            ViewBag.USER_PRIVData = userServers.GetUSER_PRIV();
            ViewBag.DEPT_IDData = userServers.GetDEPT_ID();
            ViewBag.POST_PRIVData = userServers.GetPOST_PRIV();
            ViewBag.LEAVE_DEPTData = userServers.GetLEAVE_DEPT();
                               var model = new user();
            if (id != null)
            {
                ViewBag.Title = "编辑用户";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建用户";
            }
            return View(model);
        }

        // POST: System/User/Edit/5
        [HttpPost]
        public ActionResult Edit(int? id, user model)
        {
            ViewBag.USER_PRIVData = userServers.GetUSER_PRIV();
            ViewBag.DEPT_IDData = userServers.GetDEPT_ID();
            ViewBag.POST_PRIVData = userServers.GetPOST_PRIV();
            ViewBag.LEAVE_DEPTData = userServers.GetLEAVE_DEPT();
            if (!string.IsNullOrEmpty(model.USER_PWD))
            {
                //加密密码
                model.USER_PWD = DEncryptUtil.Md5Encrypt(model.USER_PWD);
            }
            try
            {
                if (id != null)
                {
                    userServers.Update(model);
                    ViewBag.msg = "修改用户信息成功！";
                }
                else
                {

                    userServers.Add(model);
                    ViewBag.msg = "新增用户信息成功！";
                    model = new user();
                }
            }
            catch 
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: System/User/Delete/5
        public ActionResult Delete(int id)
        {
            if (id != 0)
            {
                userServers.Del(id.ToString());
                ViewBag.msg = "删除用户信息成功！";
            }
            return Content("");
        }


        #region //GetData
        public string GetTableJsonResult()
        {
            var list = userServers.GetData();
            return JsonConvert.SerializeObject(list);
        }

        public user GetModel(string id)
        {
            var model = userServers.GetModelById(id);
            return model;
        }

        #endregion //GetData
    }
}